package org.summer.lawyer.controller.wx;

import java.util.List;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.web.core.BaseController;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.lawyer.domain.vo.message.MessageVo;
import org.summer.lawyer.domain.bo.message.MessageBo;
import org.summer.lawyer.service.IMessageService;
import org.summer.common.mybatis.core.page.TableDataInfo;

/**
 * 站内消息
 *
 * @author summer
 * @date 2024-07-23
 */
@Tag(name = "微信小程序站内消息相关接口", description = "微信小程序站内消息")
@Validated
@SaCheckLogin
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/message")
public class WxMessageController {

    private final IMessageService messageService;

    /**
     * 查询站内消息列表
     */
    @Parameters({
            @Parameter(name = "receiverId", description = "消息接收人ID（登录用户ID）")
    })
    @Operation(summary = "查询站内消息列表接口", description = "查询站内消息列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @GetMapping("/list")
    public TableDataInfo<MessageVo> list(MessageBo bo, PageQuery pageQuery) {
        if(bo.getReceiverId() == null){
            bo.setReceiverId(-1L);
        }
        return messageService.selectPageList(bo, pageQuery);
    }

    /**
     * 获取站内消息详细信息
     *
     * @param id 主键
     */
    @Parameters({
            @Parameter(name = "id", description = "消息主键ID")
    })
    @Operation(summary = "获取站内消息详细信息接口", description = "查询站内消息列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @GetMapping("/{id}")
    public R<MessageVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long id) {
        return R.ok(messageService.selectMessageVoById(id));
    }


    /**
     * 删除站内消息
     *
     * @param ids 主键串
     */
    @Parameters({
            @Parameter(name = "id", description = "消息主键ID")
    })
    @Operation(summary = "删除站内消息接口", description = "删除站内消息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @DeleteMapping("/removeMessage/{ids}")
    public R<Void> removeMessage(@NotEmpty(message = "主键不能为空")
                                 @PathVariable Long[] ids) {
        messageService.deleteWithValidByIds(List.of(ids), true);
        return R.ok();
    }

    /**
     * 获取未读消息数量
     */
    @Parameters({
            @Parameter(name = "wxUserId", description = "用户ID", required = true)
    })
    @Operation(summary = "获取未读消息数量接口", description = "获取未读消息数量", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @GetMapping("/unReadMessageCount/{wxUserId}")
    public R<Long> UnReadMessageCount(@NotNull(message = "用户ID不能为空")@PathVariable("wxUserId") Long wxUserId){
        MessageBo bo = new MessageBo();
        bo.setReceiverId(wxUserId);
        return R.ok(messageService.selectUnReadMessageCount(bo));
    }

    @Parameters({
            @Parameter(name = "id", description = "消息主键ID", required = true)
    })
    @Operation(summary = "标记消息为已读接口", description = "标记消息为已读", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @PostMapping("/markAsRead")
    public R<Void> markAsRead(@RequestBody MessageBo bo){
        List<Long> ids = CollUtil.newArrayList();
        ids.add(bo.getId());
        messageService.markAsRead(ids);
        return R.ok();
    }
}
